package com.example.mybiadushoujiweishi.activity.acivitys;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;


import com.example.mybiadushoujiweishi.R;
import com.example.mybiadushoujiweishi.activity.acivitys.base.BaseActivity;
import com.example.mybiadushoujiweishi.activity.acivitys.entity.Versioninfo;
import com.example.mybiadushoujiweishi.activity.acivitys.services.VersionUpdate;
import com.example.mybiadushoujiweishi.activity.acivitys.utils.MyApplication;


import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ChekupdateActivity extends BaseActivity {

    @BindView(R.id.pic_one)
    ImageView picOne;
    @BindView(R.id.pic_two)
    ImageView picTwo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // setContentView(R.layout.activity_chekupdate);
        picTwo.setVisibility(View.GONE);
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(picOne, "scaleY", 0.5f, 5f, 0.2f);

        ObjectAnimator objectAnimatorx = ObjectAnimator.ofFloat(picOne, "scaleX", 0.5f, 5f, 0.2f);
        final AnimatorSet set = new AnimatorSet();
        set.play(objectAnimatorx).with(objectAnimator);
        set.setDuration(5000);
        set.start();


        ObjectAnimator objectAnimatortwoY = ObjectAnimator.ofFloat(picTwo, "scaleY", 0.5f, 5f, 0.2f);
        ObjectAnimator objectAnimatortwoX = ObjectAnimator.ofFloat(picTwo, "scaleX", 0.5f, 5f, 0.2f);
        final AnimatorSet settwo = new AnimatorSet();
        settwo.play(objectAnimatortwoX).with(objectAnimatortwoY);
        settwo.setDuration(5000);
        // settwo.start();

        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                picTwo.setVisibility(View.VISIBLE);
                settwo.start();
                picOne.setVisibility(View.GONE);
            }
        });
        settwo.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                picTwo.setVisibility(View.GONE);
                picOne.setVisibility(View.VISIBLE);
                set.start();
            }
        });

    }

    @Override
    public int getContentView() {
        return R.layout.activity_chekupdate;
    }

    @Override
    public void initData() {

        boolean b = checkNet();
        if (b) {
            //访问服务器，获取json数据 1有网络，但比较慢 3，网络正常可以下载 4 网络正常 已是最新版本

            System.out.println("******///////*******");
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("http://10.0.2.2:8080/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
            VersionUpdate versionUpdate = retrofit.create(VersionUpdate.class);
            Call<Versioninfo> versionlist = versionUpdate.getversion();
            versionlist.enqueue(new Callback<Versioninfo>() {
                @Override
                public void onResponse(Call<Versioninfo> call, Response<Versioninfo> response) {
               //     List<Repo> bodys= repos.body();
                    Versioninfo versioninfo=response.body();
                    int netcode=response.body().getVersioncode();
                    int oldcode= MyApplication.getVersionCode(MyApplication.getContext());
                    System.out.println(netcode);
                    System.out.println(oldcode);
                    if(netcode>oldcode)
                    {

                        System.out.println("++++++}}}}}}}}}");
                      //  setResult(3);

                        System.out.println("}}}}}}}}}}}}}}");
                        Intent intent=new Intent();
                       intent.putExtra("versioninfo",versioninfo);
                       setResult(3,intent);
                        finish();

                    }
                    else {
                        setResult(4);
                        finish();
                    }

                }

                @Override
                public void onFailure(Call<Versioninfo> call, Throwable t) {
                    setResult(1);
                    finish();
                }
            });


        } else {
            setResult(2);
            finish();
        }

    }

    private boolean checkNet() {
        NetworkInfo.State wifiState = null;
        NetworkInfo.State mobileState = null;
        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        if (wifiState != null && mobileState != null && NetworkInfo.State.CONNECTED != wifiState
                && NetworkInfo.State.CONNECTED == mobileState) {
            // 手机网络连接成功
            Toast.makeText(this, "手机网络连接成功", Toast.LENGTH_SHORT).show();
            return true;
        } else if (wifiState != null && mobileState != null
                && NetworkInfo.State.CONNECTED != wifiState
                && NetworkInfo.State.CONNECTED != mobileState) {
            // 手机没有任何的网络
            Toast.makeText(this, "手机没有任何的网络", Toast.LENGTH_SHORT).show();
            return false;
        } else if (wifiState != null && NetworkInfo.State.CONNECTED == wifiState) {
            // 无线网络连接成功
         //   Toast.makeText(this, "无线网络连接成功", Toast.LENGTH_SHORT).show();
            return true;
        }

        return false;
    }


}
